// deadzonefilter.h

#ifndef DEADZONEFILTER_H
#define DEADZONEFILTER_H

#include <filter.h>

template<typename T>
class DeadZoneFilter : public Filter<T> {
public:
	DeadZoneFilter(T width)
		: halfWidth_(width / 2)
		, initialized_(false)
		, value_(0)
	{}

	virtual ~DeadZoneFilter() {}

	virtual T update(T input) {
		if (initialized_) {
			if (input > value_ + halfWidth_)
				value_ = input - halfWidth_;
			else if (input < value_ - halfWidth_)
				value_ = input + halfWidth_;
		} else {
			value_ = input;
			initialized_ = true;
		}

		return value_;
	}

	virtual T operator()() {
		return value_;
	}

	virtual void set(T value) {
		value_ = value;
	}

private:
	DeadZoneFilter();

	bool initialized_;
	const T halfWidth_;
	T value_;
};


#endif
